package com.shouhuobao.bhi.collectpoint;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.app.Intents;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.model.ServicePointBean;
import com.umeng.analytics.MobclickAgent;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.activity.base.BaseAdapterWithImage;
import droid.frame.umeng.UMeng;
import droid.frame.utils.android.Alert;
import droid.frame.utils.lang.Str;

/**
 * 服务网点
 *
 * @author coffee<br>
 *         2016-12-4下午10:06:40
 */
public class ServicePointAdapter extends BaseAdapterWithImage<ServicePointBean> {
    /**
     * 空页面显示的提示文本
     */
    private String hitStr = "";

    public void setEmptyHitStr(String str) {
        hitStr = str;
    }

    public ServicePointAdapter(Activity context, ArrayList<ServicePointBean> items, ListView listView) {
        super(items, context, listView);
        // 地图模式 不需要设置
        if (context instanceof ServicePointMapActivity == false) {
            setEmptyView(R.layout.service_point_empty);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            View emptyLayout = super.getView(position, convertView, parent);
            TextView hitText = (TextView) emptyLayout.findViewById(R.id.hit_text);
            if (!Str.isEmpty(hitStr)) {
                hitText.setText(hitStr);
            }
            return emptyLayout;
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.service_point_item, parent, false);
            holder.logo = (ImageView) convertView.findViewById(R.id.service_point_logo);
            holder.name = (TextView) convertView.findViewById(R.id.service_point_name);
            holder.type1 = (TextView) convertView.findViewById(R.id.service_point_type1);
            holder.type2 = (TextView) convertView.findViewById(R.id.service_point_type2);
            holder.address = (TextView) convertView.findViewById(R.id.service_point_address);
            holder.serviceGoto = convertView.findViewById(R.id.service_point_goto);
            holder.call = convertView.findViewById(R.id.service_point_call);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ServicePointBean item = getItem(position);
        // 驿站
        if (item.isStation()) {
            ((ViewGroup) holder.call.getParent()).setVisibility(View.VISIBLE);
            if (Str.isEmpty(item.getHeadImage())) {
                holder.logo.setImageResource(R.drawable.service_point_logo2);
            } else {
                showImage(item.getHeadImage(), holder.logo, null);
                holder.logo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogMgr.showLargeStationPhoto(context, item.getHeadImage());
                    }
                });
            }
        } else {
            ((ViewGroup) holder.call.getParent()).setVisibility(View.GONE);
            holder.logo.setImageResource(R.drawable.service_point_logo1);
        }
        holder.name.setText(item.getName());
        // if (item.getPostCooperationType() == 2) {
        // holder.type1.setVisibility(View.VISIBLE);
        // holder.type2.setVisibility(View.VISIBLE);
        // holder.type1.setText("代收点");
        // holder.type2.setText("投递点");
        // } else {
        // holder.type1.setVisibility(View.GONE);
        // holder.type2.setVisibility(View.VISIBLE);
        // if (item.getPostCooperationType() == 1) {
        // holder.type2.setText("投递点");
        // } else {
        // holder.type2.setText("代收点");
        // }
        // }
        holder.address.setText(item.getAddress());
        //
        holder.serviceGoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMeng.click(context, "servicePoint_goto_map_click");
                if (!Str.isEmpty(item.getLatitude()) && !Str.isEmpty(item.getLongitude())) {
                    Intents.navigation(item.getLatitude(), item.getLongitude(), //
                            item.getGdlatitude(), item.getGdLongitude(),item.getAddress());
                }else {
                    Alert.toast("坐标有误");
                }

            }
        });
        holder.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intents.dialOrCall(item.getPhone());
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView logo;
        TextView name;
        TextView type1, type2;// 代收点、投递点
        TextView address;
        View serviceGoto;// 服务网点
        View call;// 拨打电话
    }

}
